package com.mmaingenieria.GestorFacturacion.listener;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * Application Lifecycle Listener implementation class Listener
 *
 */
public class Listener implements ServletContextListener {

	//Atributos
	private static final String path_properties = "WEB-INF/config/log4j.properties";
	static Properties propError = new Properties();	
	
    /**
     * Default constructor. 
     */
    public Listener() {
        // TODO Auto-generated constructor stub
    }

    public static Properties getErrorProperties(){
    	return propError;
    }
    
	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce) {
    	
    	
    	String servletRealPath = sce.getServletContext().getRealPath("/");
		PropertyConfigurator.configure( servletRealPath + path_properties);
		Logger.getLogger(this.getClass() ).info("Iniciamos contexto Listener ");
		
		try
		{
	    	String error=sce.getServletContext().getInitParameter("errores");
	    	propError.load(sce.getServletContext().getResourceAsStream(error));
	    	System.out.println(Listener.getErrorProperties().getProperty("pruebaProperties"));
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent sce) {
    	Logger.getLogger(this.getClass() ).info("Destruimos contexto Listener ");
    }
	
}
